﻿function parseDouble(value) {

    //alert('value ' + value);
    if (typeof value == "string") {
        value = value.split(',').join('');
        // value = value.match(/^-?\d*/)[0];    
    }
    return !isNaN(parseFloat(value)) ? value * 1 : NaN;
}
function FormatNumber0(value) {
    return $.number(value, 0)
}

function FormatNumber2(value) {
    return $.number(value, 2)
}


function FormatNumber5(value) {
    return $.number(value, 5)
}

function FormatNumber6(value) {
    return $.number(value, 6)
}


//ทศนิยม 2 ตำแหน่ง
function FormatNumber2Digits() {
    //alert('1 '+ $(this).val()); 
    if ($(this) != null) {
        var value = $(this).val();
        if (value != '' && value != null) {
            //alert('x ' + parseFloat(value).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
            $(this).val(parseDouble(value).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
        }
    }
}
function getFormatNumber2Digits(value) {
    //var x = o.val();
    //o.val(FormatNumber2(parseDouble(x)));  
    //alert('test1 '); 
    if (value != '' && value != null) {
        return  parseDouble(value).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString();
    }
    return '';
}

function FormatNumber2DigitsWithParam(o) {
    //var x = o.val();
    //o.val(FormatNumber2(parseDouble(x)));  
    //alert('test1 '); 
    if (o != null) {
        var value = o.val();
        if (value != '' && value != null) {
            o.val(parseDouble(value).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
        }
    }
}



//ไม่กำหนด ทศนิยม แต่จัด format ให้ 
function FormatNumberFreeDigits() {
    if ($(this) != null) {
        var value = $(this).val();
        if (value != '' && value != null) {
            var parts = value.toString().split(".");
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            $(this).val(parts.join("."));
        }
    }
}

function FormatNumberFreeDigitsWithParam(o) {
    if (o != null) {
        var value = o.val();
        if (value != '' && value != null) {
            var parts = value.toString().split(".");
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            o.val(parts.join("."));
        }
    }
}


//ไม่มีทศนิยม และจัด format ให้ 
function FormatCurrencyNoDigits() {
    if ($(this) != null) {
        var value = $(this).val();
        if (value != '' && value != null) {
            var parts = value.toString().split(".");
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            $(this).val(parts);
        }
    }
}
function FormatCurrencyNoDigitsWithParam(o) {
    if (o != null) {
        var value = o.val();
        if (value != '' && value != null) {
            var parts = value.toString().split(".");
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            o.val(parts);
        }
    }
}

function isDate(txtDate) {
    var currVal = txtDate;
    if (currVal == '')
        return null;

    var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
    var dtArray = currVal.match(rxDatePattern); // is format OK?

    if (dtArray == null)
        return 'รูปแบบวันที่ไม่ถูกต้อง';

    //Checks for dd/mm//yyyy format.
     dtDay = dtArray[1];
     dtMonth  = dtArray[3];
    dtYear = dtArray[5];

    if (dtMonth < 1 || dtMonth > 12)
        return 'เดือนไม่ถูกต้อง';
    else if (dtDay < 1 || dtDay > 31)
        return 'วันที่ไม่ถูกต้อง';
    else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
        return 'วันที่หรือเดือนไม่ถูกต้อง';
    else if (dtMonth == 2) {
        var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
        if (dtDay > 29 || (dtDay == 29 && !isleap))
            return 'ปีไม่ถูกต้อง';
    }
    return null;
}


function applyValidateDate()
{
    $("[need-validateDate]").blur(function (event)
    {
        var dateVal = $(this).val();
        var errorMsg = isDate(dateVal);
        if (errorMsg != null)
        {
            showErrorDlg(errorMsg);
            $(this).focus();
        }
        
    });
}



